<template>
	<view>
		<u-popup :show="show" @close="close" mode='center' :round="10">
			<view class="popupCla">
				<view class="addTitleCla">{{addTitle}}</view>
				<view class="popupContext">
					<u--textarea class="textareaCla" v-model="value1.Address"></u--textarea>
					<view class="mapCla" @click="addmapClick(addxxAddressItem)">
						<u-icon name="map-fill" color="#2979ff" size="24"></u-icon>
						<view class="">定位</view>
					</view>
				</view>
				<view class="btnCla">
					<u-button class="buttonCla" type="primary" text="保存地址" @click="addSaveClick"></u-button>
				</view>
				
			</view>
		</u-popup>
	</view>
</template>

<script>
	import area from '@/pages/component/area.js'
	export default {
		data() {
			return {
				value1: {

				},
			}
		},
		props: {
			show: {
				type: Boolean,
				default: false
			},
			addTitle: {
				type: String,
			},
			addxxAddressItem: {
				type: Object,
				default: {}
			},
		},
		created() {
			this.value1 = JSON.parse(JSON.stringify(this.addxxAddressItem));
			if (!this.value1.Address) {
				this.addmapClick(this.addxxAddressItem);
			}
		},
		methods: {
			close() {
				this.$emit('update:show', false)
			},
			addmapClick(addxxAddressItem) {
				let that = this;
				uni.chooseLocation({
					latitude: Number(addxxAddressItem.Latitude),
					longitude: Number(addxxAddressItem.Longitude),
					success: function(res) {
						//经度
						addxxAddressItem.Longitude = res.longitude;
						//纬度
						addxxAddressItem.Latitude = res.latitude;
						// item.PcaAddress = res.name;
						// addxxAddressItem.Address = res.address;
						that.value1.Address = res.address;
						that.geocoddata(res, addxxAddressItem);
						//逆地理编码  获取区和街道
						let obj = {}
						obj.Location = addxxAddressItem.Longitude + ',' + addxxAddressItem.Latitude;
						obj.Key = that.$config.domain.aMapWebServerKey,
							that.$http.core.aMapRegeo(obj).then((res) => {
								if (res.Success) {
									let district = res.Result.addressComponent.district //区
									let businessAreas = res.Result.addressComponent.businessAreas[0]
										.name //地名
									that.value1.PcaAddress = district + ' ' + businessAreas
								}
							})
					},
					fail: function(e) {}
				})
			},
			//根据经纬度 获取信息
			geocoddata(e, dataitem) {
				let that = this;
				let location = e.longitude + ',' + e.latitude;
				that.$util.geocoderegeo(location).then(res => {
					//详细地址信息
					console.log(res, '11111111111111');
					//定位地址
					//	_that.current_wz = res.data.regeocode.formatted_address
					let adcode = res.data.regeocode.addressComponent.adcode
					area.forEach((item, index) => {
						item.children?.forEach((item1, index) => {
							item1.children?.forEach((item2, index) => {
								if (adcode == item2.value) {
									this.value1.ProvinceCode = item.value;
									this.value1.CityCode = item1.value;
									this.value1.AreaCode = item2.value;
								}
							})
						})
					})
				})
			},
			//保存地址按钮
			addSaveClick() {
				this.$emit('editAddress', this.value1)
			}
		}
	}
</script>

<style lang="scss" scoped>
	.popupCla {
		width: 90vw;

		.addTitleCla {
			margin: 20rpx 0;
			display: flex;
			justify-content: center;
			font-size: 34rpx;
		}

		.popupContext {
			display: flex;
			align-items: center;

			.textareaCla {
				margin-left: 40rpx;
				width: 80%;
			}

			.mapCla {
				width: 20%;
				display: flex;
				flex-direction: column;
				align-items: center;
				font-size: 26rpx;
			}
		}
		.btnCla{
			display: flex;
			justify-content: center; 
			.buttonCla {
				width: 80%;
				margin: 30rpx 0;
				
			}
		}
		
	}
</style>